package com.lagou.rpc.consumer.aop;

import ch.qos.logback.core.util.DatePatternToRegexUtil;
import cn.hutool.core.date.DateUtil;
import org.I0Itec.zkclient.ZkClient;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

public class WebLogAcpect {
    private Logger logger = LoggerFactory.getLogger(WebLogAcpect.class);

    @Pointcut("execution(public * com.lagou.rpc.consumer.controller..*.*(..))")//切入点描述 这个是controller包的切入点.MyController
    public void controllerLog() {
    }

    @Around("controllerLog()") //在切入点的方法run之前要干的
    public Object doAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        long start = System.currentTimeMillis();
        ZkClient zkClient = new ZkClient("192.168.200.20:2181");
        logger.info("--------doAround ----------  ");
        //执行目标方法
        Object obj = proceedingJoinPoint.proceed();
        //只有返回了obj，@AfterReturning才获取返回的结果，否则哪里的result为null
        String path = "/lg-rpc-consumer";
        long end = System.currentTimeMillis();
        long cost = end - start;
        String now = DateUtil.now();
        logger.info("本次花费时间：" + cost + "ms" + ";本此结束时间为：" + now);
        return obj;
    }

}
